import {
  getListData,
  preRequestOuterData,
  getExternalFormInfo,
  isExternalForm,
  viewExternalFormInfo,
  FormSetupInfo,
} from "../pages/flowmanage/formconfigure/server/formConfigureApi";

const initData = {
  listData: [],
  totalCount: 0,
  externalFormInfoData: {},
};

export default {
  namespace: "FormSelectModel",
  state: {
    ...initData,
  },

  effects: {
    *getListData({ payload }, { call, put }) {
      const resData = yield call(getListData, payload);
      const { datas = [], totalCount = 0, errCode, errMsg } = resData || {};
      yield put({ type: "get/listData", payload: datas || [], totalCount });
      return { errCode, errMsg };
    },
    *preRequestOuterData({ payload }, { call }) {
      const resData = yield call(preRequestOuterData, payload);
      const { errCode, errMsg, datas = {}, errortext } = resData || {};

      return { errCode, errMsg: errortext || errMsg, datas };
    },
    *getExternalFormInfo({ payload }, { call }) {
      const resData = yield call(getExternalFormInfo, payload);
      const { errCode, errMsg, datas = {}, errortext } = resData || {};
      return { errCode, errMsg: errortext || errMsg, datas };
    },
    *isExternalForm({ payload }, { call, put }) {
      const resData = yield call(isExternalForm, payload);
      const { errCode, errMsg, datas = {}, errortext } = resData || {};
      yield put({ type: "get/isExternalForm", payload: datas });
      return { errCode, errMsg: errortext || errMsg, datas };
    },
    *viewExternalFormInfo({ payload }, { call }) {
      const resData = yield call(viewExternalFormInfo, payload);
      const { errCode, errMsg, datas = {}, errortext } = resData || {};
      return { errCode, errMsg: errortext || errMsg, datas };
    },
    *FormSetupInfo({ payload }, { call }) {
      const resData = yield call(FormSetupInfo, payload);
      const { errCode, errMsg, datas = {}, errortext } = resData || {};
      return { errCode, errMsg: errortext || errMsg, datas };
    },
  },
  reducers: {
    "get/listData": (state, { payload, totalCount }) => ({
      ...state,
      listData: payload,
      totalCount,
    }),
    // "get/isExternalForm": (state, { payload }) => ({
    //   ...state,
    //   isExternalForm: payload,
    // }),
    clearData: () => ({ ...initData }),
  },
};
